package com.wu.common.utils.site;


import android.text.TextUtils;

import com.wu.common.utils.net.UsTvShowDataCallBack;
import com.wu.common.utils.net.XnbNetUtil;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 作者:吴奎庆
 * <p>
 * 时间:2020/5/17
 * <p>
 * 用途:
 */


public class XNBSite {

    public static String baseUrl = "https://www.528btc.com/";

    public static void getXNB(UsTvShowDataCallBack<List<XnbInfo>> callBack) {

        Observable.create(new ObservableOnSubscribe<List<XnbInfo>>() {
            @Override
            public void subscribe(ObservableEmitter<List<XnbInfo>> emitter) throws Exception {

                String html = XnbNetUtil.getHtml(baseUrl, "UTF-8");

                if (!TextUtils.isEmpty(html)) {
                    Document document = Jsoup.parse(html);
                    List<XnbInfo> lists = new ArrayList<>();
                    if (document.select("tbody") != null && document.select("tbody").size() > 0) {
                        Elements elements = document.select("tbody").first().children();

                        for (Element element : elements) {
                            XnbInfo info = new XnbInfo();
                            info.setPosition(element.getElementsByClass("market_name").first().text());
                            String[] names = element.getElementsByClass("name").first().text().split(" ");
                            if (names.length>=2){
                                info.setXnbName(names[1]);
                            }
                            info.setXnbEName(names[0]);

                            String[] prices = element.getElementsByClass("main").text().split(" ");
                            info.setXnbIcon(element.getElementsByClass("market_name").get(1).select("img").first().attr("src"));
                            info.setXnbRmbPrice(prices[0]);
                            info.setXnbUsaPrice(prices[1]);
                            info.setXnb24JYE(prices[2]);
                            info.setXnb24ZF(prices[3]);
                            info.setXnbLT(element.select("td").get(4).text());
                            lists.add(info);
                        }
                    }
                    if (lists.size() == 0) {
                        emitter.onError(new Throwable("数据异常"));
                    } else {
                        emitter.onNext(lists);
                    }

                } else {
                    emitter.onError(new Throwable("数据异常"));
                }


            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<XnbInfo>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(List<XnbInfo> homeInfo) {
                        callBack.onSuccess(homeInfo);

                    }

                    @Override
                    public void onError(Throwable e) {
                        callBack.onFail(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });


    }


}
